package com.lxmes.jiesuan.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.business.domain.CcCaigouMain;
import com.lxmes.business.domain.CcChukuMain;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.service.ICcChukuMainService;
import com.lxmes.jiesuan.domain.JiesuanZhikuan;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.jiesuan.domain.JiesuanShoukuan;
import com.lxmes.jiesuan.service.IJiesuanShoukuanService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 收款记账Controller
 * 
 * @author 张志煜
 * @date 2022-09-26
 */
@Api(tags="结算中心-收款")
@RestController
@RequestMapping("/jiesuan/shoukuan")
public class JiesuanShoukuanController extends BaseController
{
    @Autowired
    private IJiesuanShoukuanService jiesuanShoukuanService;
    @Autowired
    private ICcChukuMainService ccChukuMainService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询收款记账列表
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:list')")
    @GetMapping("/list")
    public TableDataInfo list(JiesuanShoukuan jiesuanShoukuan)
    {
        startPage();
        List<JiesuanShoukuan> list = jiesuanShoukuanService.selectJiesuanShoukuanList(jiesuanShoukuan);
        return getDataTable(list);
    }
    /**
     * 手机端采购单列表
     */
    @ApiOperation(value="结算中心支收款列表")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ywType", value = "业务类型", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "ywId", value = "业务单号", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "ywId2", value = "订单号", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cusId", value = "客户名称", paramType="query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "skfs", value = "收款方式", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "querySkDate1", value = "收款开始时间", paramType="query", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "querySkDate2", value = "收款结束时间", paramType="query", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = CcCaigouMain.class)})
    @PreAuthorize("@ss.hasPermi('mobile:zhikuan:list')")
    @GetMapping("/mobileList")
    public TableDataInfo mobileList(
            @RequestParam(value ="ywType",required = false) String ywType,
            @RequestParam(value ="ywId",required = false) String ywId,
            @RequestParam(value ="ywId2",required = false) String ywId2,
            @RequestParam(value ="cusId",required = false) Long cusId,
            @RequestParam(value ="skfs",required = false) String skfs,
            @RequestParam(value ="queryZfDate1",required = false) Date querySkDate1,
            @RequestParam(value ="queryZfDate2",required = false) Date querySkDate2
    )
    {
        startPage();
        JiesuanShoukuan jiesuanShoukuan=new JiesuanShoukuan();
        jiesuanShoukuan.setYwType(ywType);
        jiesuanShoukuan.setYwId(ywId);
        jiesuanShoukuan.setYwId2(ywId2);
        jiesuanShoukuan.setCusId(cusId.toString());
        jiesuanShoukuan.setSkfs(skfs);
        jiesuanShoukuan.setQuerySkDate1(querySkDate1);
        jiesuanShoukuan.setQuerySkDate2(querySkDate2);
        List<JiesuanShoukuan> list = jiesuanShoukuanService.selectJiesuanShoukuanList(jiesuanShoukuan);
        return getDataTable(list);
    }


    /**
     * 导出收款记账列表
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:export')")
    @Log(title = "收款记账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JiesuanShoukuan jiesuanShoukuan)
    {
        List<JiesuanShoukuan> list = jiesuanShoukuanService.selectJiesuanShoukuanList(jiesuanShoukuan);
        ExcelUtil<JiesuanShoukuan> util = new ExcelUtil<JiesuanShoukuan>(JiesuanShoukuan.class);
        util.exportExcel(response, list, "收款记账数据");
    }

    /**
     * 获取收款记账详细信息
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jiesuanShoukuanService.selectJiesuanShoukuanById(id));
    }
    /**
     * 手机端获取收款明细细信息
     */
    @ApiOperation(value="收款明细细信息")
    @ApiImplicitParam(name = "id", value = "收款id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/getShouKuan/{id}")
    public AjaxResult mobileGetInfo(@PathVariable("id") Long  id)
    {
        return AjaxResult.success(jiesuanShoukuanService.selectJiesuanShoukuanById(id));
    }


    @GetMapping(value = "/getSurplusShoukuan/{ywId}")
    public AjaxResult getSurplusShoukuan(@PathVariable("ywId") String ywId)
    {
        return AjaxResult.success(jiesuanShoukuanService.selectJsSurplusShoukuan(ywId));
    }

    /**
     * 新增收款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:add')")
    @Log(title = "收款记账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JiesuanShoukuan jiesuanShoukuan)
    {
        return toAjax(jiesuanShoukuanService.insertJiesuanShoukuan(jiesuanShoukuan));
    }
    /**
     * 新增收款记账
     */
    @ApiOperation(value="收款记账")
    @PreAuthorize("@ss.hasPermi('mobile:shoukuan:add')")
    @Log(title = "新增收款记账", businessType = BusinessType.INSERT)
    @PostMapping("/mobileAdd")
    public AjaxResult mobileAdd(@RequestBody JiesuanShoukuan jiesuanShoukuan)
    {
        return toAjax(jiesuanShoukuanService.insertJiesuanShoukuan(jiesuanShoukuan));
    }
    @ApiOperation(value="获取收款业务类型")
    @GetMapping("/getSkYwType")
    public AjaxResult getSkYwType(){
        return AjaxResult.success( dictDataService.selectDictDataByType("sk_yw_type"));
    }
    @ApiOperation(value="获取收款业务单号",tags = " 出库方式 2-产成品")
    @ApiImplicitParam(name = "chukuFlag", value = "出库方式", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping("/getSkYwDan/{chukuFlag}")
    public AjaxResult getSkYwDan(@PathVariable("chukuFlag") String  chukuFlag){
        List<CcChukuMain> list = ccChukuMainService.selectChukumainBychukuFlag(chukuFlag);
        JSONArray jsonArray=new JSONArray();
        for (CcChukuMain main:list) {
            JSONObject object=new JSONObject();
            object.put("value",main.getDanhao());
            object.put("label",main.getOrderTitle());
            object.put("cusName",main.getCusName());
            object.put("cusId",main.getCusId());
            object.put("orderId",main.getOrderId());
            object.put("money",main.getTotalPrice());
            jsonArray.add(object);
        }
        return AjaxResult.success(jsonArray);
    }
    @ApiOperation(value="获取收款方式")
    @GetMapping("/getSkFs")
    public AjaxResult getSkFs(){
        return AjaxResult.success( dictDataService.selectDictDataByType("sz_fs"));
    }

    /**
     * 修改收款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:edit')")
    @Log(title = "收款记账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JiesuanShoukuan jiesuanShoukuan)
    {
        return toAjax(jiesuanShoukuanService.updateJiesuanShoukuan(jiesuanShoukuan));
    }

    /**
     * 删除收款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:shoukuan:remove')")
    @Log(title = "收款记账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jiesuanShoukuanService.deleteJiesuanShoukuanByIds(ids));
    }
}
